<template>
	<view>
		<dialog-shell ref="shell" title="用户登录" confirmText="登录" @confirm="clickLoginCommit">
			<image src="../../static/icons/icon.png" class="ic-welcome"></image>
			<text class="welcone">WELCOME!</text>
			<text class="info-register" @click.stop="clickGoRegister">没有账号？请注册！</text>
			<view class="input-container">
				<input type="text" placeholder="请输入用户名" :value="userValue" placeholder-class="input-holder"
					@input="inputGetName" />
				<text class="iconfont icon-yonghu ic-user"></text>
			</view>
			<view class="input-container" style="margin-bottom: 20rpx;">
				<input v-if="isPwd" type="password" :value="pwdValue" placeholder="请输入密码!"
					placeholder-class="input-holder" @input="inputGetPwd" />
				<input v-else type="text" :value="pwdValue" placeholder="请输入密码!" placeholder-class="input-holder" />
				<text :class="isPwd ? 'iconfont icon-yanjing-kai ic-pwd' : 'iconfont icon-yanjing-guan ic-pwd'"
					@click="clickChangePwd"></text>
				<text class="iconfont icon-yaochi ic-user"></text>
			</view>
		</dialog-shell>
	</view>
</template>

<script>
	export default {
		name: "login-dialog",
		data() {
			return {
				isPwd: true,
				pwdValue: "",
				userValue: ""
			};
		},
		mounted() {
			let userInfo = uni.getStorageSync("userinfo")
			if (userInfo) {
				this.userValue = userInfo.username
				this.pwdValue = userInfo.password
			}
		},
		methods: {
			show() {
				this.$refs.shell.showDialog()
			},
			hide() {
				this.$refs.shell.clickToHide()
			},
			clickChangePwd() {
				this.isPwd = !this.isPwd
			},
			inputGetPwd(e) {
				this.pwdValue = e.detail.value
			},
			inputGetName(e) {
				this.userValue = e.detail.value
			},
			clickGoRegister() {
				uni.navigateTo({
					url: '/pages/register/register',
					success: res => {
						this.$refs.shell.clickToHide()
					}
				});
			},
			clickLoginCommit() {
				if (this.userValue.length == 0 || this.pwdValue.length == 0) {
					uni.showToast({
						title: '请输入用户名和密码',
						icon: 'none'
					});
					return
				}
				let url = this.$params.host + this.$params.action_login
				let data = {
					username: this.userValue,
					password: this.pwdValue
				}
				console.log(data)
				this.$request.post(url, data, res => {
					console.log(res)
					if (!res.success) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
						this.$refs.shell.showDialog()
						return
					}
					uni.setStorageSync("userinfo", data)
					let app = getApp()
					app.globalData.uid = res.data.id
					app.globalData.avatar = res.data.avatar
					app.globalData.email = res.data.email
					app.globalData.userName = res.data.userName
					app.globalData.nickName = res.data.nickName
					app.globalData.type = res.data.type
					app.globalData.token = res.data.token
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					this.$emit("after")
					this.$noti.post(this.$params.noti_login_status, false)
				})
			}
		}
	}
</script>

<style>
	@import url("login-dialog.css");
</style>
